package ipdesk.model.report.seminar.action;

import ipdesk.model.report.seminar.bean.SeminarInfoBean;
import ipdesk.model.report.seminar.dao.SeminarInfoDao;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ecg.itf.dao.ECGDao;
import ecg.util.FileService;
import ecg.util.SqlMapLocator;
import ecg.util.StringUtils;
import ecg.util.Time;

public class SeminarInfoRegAction extends ActionSupport implements Preparable, ModelDriven<SeminarInfoBean>
{
	private SeminarInfoBean seminarInfoBean;
	FileService fileService = new FileService();
	
	public String execute() throws Exception
	{
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.seminar");
		String totalPath = uplodaPathPrefix + basePath;
		
		String regTime = Time.getDateTime();
		
		seminarInfoBean.setPathName(basePath);
		seminarInfoBean.setRegTime(regTime);
		seminarInfoBean.setDiscussIssue(StringUtils.nl2br(seminarInfoBean.getDiscussIssue()));
		seminarInfoBean.setDeskCode(seminarInfoBean.getDeskCode());		
		System.out.println("***" + seminarInfoBean.getDeskCode());
		// 상담정보 insert.
		int lastSeminarSeq = SeminarInfoDao.getInstance().seminarInfoReg(seminarInfoBean);
	
		// 마지막으로 insert된 seminar_seq 값을 빈 객체에 저장.		
		seminarInfoBean.setSeminarSeq(lastSeminarSeq);
	
		if(lastSeminarSeq != 0 && seminarInfoBean.getUpload() != null)
		{
			String uploadFileName = seminarInfoBean.getUploadFileName();
			System.out.println("1. "+uploadFileName);
			String realFileName = fileService.saveFile(seminarInfoBean.getUpload(), totalPath, uploadFileName);
		
			seminarInfoBean.setUploadFileName(realFileName);
			seminarInfoBean.setFileCategory("FLE03");
			
			SeminarInfoDao.getInstance().insertFileData(seminarInfoBean);
		}
		
		if(lastSeminarSeq != 0 && seminarInfoBean.getUploadPic() != null)
		{
			String uploadPicName = seminarInfoBean.getUploadPicFileName();
			System.out.println("2. "+uploadPicName);
			String realPicName = fileService.saveFile(seminarInfoBean.getUploadPic(), totalPath, uploadPicName);
		
			seminarInfoBean.setUploadFileName(realPicName);
			seminarInfoBean.setFileCategory("FLE02");
			
			SeminarInfoDao.getInstance().insertFileData(seminarInfoBean);
		}
		
		return SUCCESS;
	}
	
	public void prepare() throws Exception
	{
		seminarInfoBean = new SeminarInfoBean();
	}
	
	public SeminarInfoBean getModel()
	{
		return seminarInfoBean;
	}
	
	/*public void validate()
	{
		String contentType = seminarInfoBean.getUploadContentType();
		long fileSize = 0;
		
		if(seminarInfoBean.getUpload() != null)
		{
			fileSize = seminarInfoBean.getUpload().length();
		}
		
		if(!fileService.contentTypeValidate(contentType))
		{
			addFieldError("upload", "업로드할 수 없는 파일 형식입니다");
		}
		
		if(fileSize > Long.parseLong(getText("upload.size")))
		{
			addFieldError("upload", "업로드 용량을 초과하였습니다.");
			return;
		}
	}*/
}
